package com.erpanderp.wam.usm.view.timesheet;

import java.io.Serializable;
import java.text.DateFormatSymbols;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.swing.SwingWorker;

import com.erpanderp.wam.usm.model.entity.timesheet.Timesheet;
import com.erpanderp.wam.usm.model.entity.user.Employee;
import com.erpanderp.wam.usm.model.service.timesheet.TimesheetDAO;
import com.erpanderp.wam.usm.model.service.timesheet.TimesheetDetailDAO;
import com.erpanderp.wam.usm.model.service.user.EmployeeDAO;
import com.erpanderp.wam.usm.model.service.user.EmployeeRoleDAO;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.EmailUtil;
import com.erpanderp.wam.util.WAMHostManager;
@ManagedBean
@ViewScoped

public class TimesheetApprovalRamainderBean implements Serializable {

	private static final long serialVersionUID = 1L;
	HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
	HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
	ServletContext context = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
	
	private EmailUtil emailUtil = new EmailUtil();
	private Employee employee = new Employee();
	private Timesheet timesheet = new Timesheet();
	private List<String> getAllProjectManagersEmailIds = new LinkedList<String>();
	private List<Employee> employeeDetails = new LinkedList<Employee>();
	

	EmployeeDAO employeeDAO = new EmployeeDAO();
	EmployeeRoleDAO employeeRoleDAO = new EmployeeRoleDAO();
	

	TimesheetDAO timesheetDAO = new TimesheetDAO();
	TimesheetDetailDAO timesheetDetailDAO = new TimesheetDetailDAO();
	
    Calendar calendar = Calendar.getInstance();
	String EMAIL_MESSAGE;
	String monthNames[] = new DateFormatSymbols().getMonths();
	String timesheetMonth = monthNames[calendar.get(Calendar.MONTH)].toUpperCase();
	String year = String.valueOf(calendar.get(Calendar.YEAR));
	String emailTo;
	public TimesheetApprovalRamainderBean()
	{
		System.out.println("in con.............");
		System.out.println("AT CALLER METHOD: " + sendMessage());
	}
	
	//Remainder mail
	public String sendMessage(){
		
		Map<String, String> hostsMap = WAMHostManager.getWAMHosts(context);
		String domainName = context.getInitParameter("domainName");
		employeeDetails = employeeDAO.getAllEmployees();
		getAllProjectManagersEmailIds = employeeRoleDAO.getAllProjectManagersEmailIds();
		
			
		for(String projectManagerEmailId :getAllProjectManagersEmailIds)
		  {
		 setEmailTo(projectManagerEmailId);
		 System.out.println("eamilto......."+emailTo);
		 
		 
		//// String[] to ={emailTo}; 
		 
		 if(hostsMap.get(CommonLookup.DEV_EMAIL) == null){

				String[] to = {emailTo};
				emailUtil.setRecieverAddresses(to);
			}else{
				System.out.println("Email: " + hostsMap.get(CommonLookup.DEV_EMAIL));
				String[] to = {hostsMap.get(CommonLookup.DEV_EMAIL)};
				System.out.println(to);
				emailUtil.setRecieverAddresses(to);
			} 
		 
		 
			String requestURL = "usm/timesheet/viewtimesheets.html";
			String privateLink = "http://" + hostsMap.get(CommonLookup.PRIVATEHOST) + "/" + domainName + "/" + requestURL+"?projectManagerEmailId="+projectManagerEmailId;
			String publicLink = "http://" + hostsMap.get(CommonLookup.PUBLICHOST) + "/"	+ domainName + "/" + requestURL+"?projectManagerEmailId="+projectManagerEmailId;
			String messageString ="Gentle Reminder for the Monthly Time sheet-"  + "("+
					timesheetMonth+" "+year+")"+"!!!"+"<br>"+"<br>"+
					"Reporting Managers are requested to approve employees timesheets through the online timesheet tool and the last date for submission is on or before 29th."+"<br><br>"+
					"Please note that tool will not allow approving timesheet after 29th of the month."+"<br>"+"<br>"+
					"<a style='color:#FF9933;font-weight: bold;' href=\""+
					privateLink + "\">" + "Please click here to approve/reject timesheet"+ "</a /><br />"+
					"(Employees who are in Hyderabad office)"+"<br /><br />"+
					"<a style='color:#FF9933;font-weight: bold;' href=\""+
					publicLink + "\">" + "Please click here to approve/reject timesheet"+ "</a /><br />"+
					"(Out side of Hyderabad office)"+"<br /><br />"+

					"Thanks & Regards, <br />"
					+ CommonLookup.WAM_DEVTEAM_SIGNATURE;
					
			emailUtil.setMessage(messageString);
			//emailUtil.setRecieverAddresses(to);
			emailUtil.setSenderEmail("podquick@erpanderp.com", "WAM - Dev Team");
			emailUtil.setSubject("Alert-Information:Reminder of Timesheet Approval");

			SwingWorker worker = new SwingWorker<String, Object>() {
			@Override
			protected String doInBackground() throws Exception {
				EMAIL_MESSAGE = emailUtil.processAndSendEmail();
				return null;
			}

			@Override
			protected void done() {
					System.out.println("DONE " + EMAIL_MESSAGE);		
			}
			};
			worker.execute();
			System.out.println( "BEFORE RETURNING: " + EMAIL_MESSAGE);
		  }
		
		
		return EMAIL_MESSAGE;
	}
	//GETTERS AND SETTERS
	public HttpSession getSession() {
		return session;
	}
	public void setSession(HttpSession session) {
		this.session = session;
	}
	public HttpServletRequest getRequest() {
		return request;
	}
	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}
	public ServletContext getContext() {
		return context;
	}
	public void setContext(ServletContext context) {
		this.context = context;
	}
	public EmailUtil getEmailUtil() {
		return emailUtil;
	}
	public void setEmailUtil(EmailUtil emailUtil) {
		this.emailUtil = emailUtil;
	}
	public EmployeeDAO getEmployeeDAO() {
		return employeeDAO;
	}
	public void setEmployeeDAO(EmployeeDAO employeeDAO) {
		this.employeeDAO = employeeDAO;
	}
	public String getEMAIL_MESSAGE() {
		return EMAIL_MESSAGE;
	}
	public void setEMAIL_MESSAGE(String eMAIL_MESSAGE) {
		EMAIL_MESSAGE = eMAIL_MESSAGE;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public Timesheet getTimesheet() {
		return timesheet;
	}

	public void setTimesheet(Timesheet timesheet) {
		this.timesheet = timesheet;
	}

	public TimesheetDAO getTimesheetDAO() {
		return timesheetDAO;
	}

	public void setTimesheetDAO(TimesheetDAO timesheetDAO) {
		this.timesheetDAO = timesheetDAO;
	}

	public TimesheetDetailDAO getTimesheetDetailDAO() {
		return timesheetDetailDAO;
	}

	public void setTimesheetDetailDAO(TimesheetDetailDAO timesheetDetailDAO) {
		this.timesheetDetailDAO = timesheetDetailDAO;
	}

	public Calendar getCalendar() {
		return calendar;
	}

	public void setCalendar(Calendar calendar) {
		this.calendar = calendar;
	}

	public Employee getEmployee() {
		return employee;
	}
	public void setEmployee(Employee employee) {
		this.employee = employee;
	}
	public String[] getMonthNames() {
		return monthNames;
	}

	public void setMonthNames(String[] monthNames) {
		this.monthNames = monthNames;
	}

	public String getTimesheetMonth() {
		return timesheetMonth;
	}

	public void setTimesheetMonth(String timesheetMonth) {
		this.timesheetMonth = timesheetMonth;
	}

	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}
	public String getEmailTo() {
		return emailTo;
	}

	public void setEmailTo(String emailTo) {
		this.emailTo = emailTo;
	}
	public List<String> getGetAllProjectManagersEmailIds() {
		return getAllProjectManagersEmailIds;
	}

	public void setGetAllProjectManagersEmailIds(
			List<String> getAllProjectManagersEmailIds) {
		this.getAllProjectManagersEmailIds = getAllProjectManagersEmailIds;
	}
	public EmployeeRoleDAO getEmployeeRoleDAO() {
		return employeeRoleDAO;
	}

	public void setEmployeeRoleDAO(EmployeeRoleDAO employeeRoleDAO) {
		this.employeeRoleDAO = employeeRoleDAO;
	}

	public List<Employee> getEmployeeDetails() {
		return employeeDetails;
	}

	public void setEmployeeDetails(List<Employee> employeeDetails) {
		this.employeeDetails = employeeDetails;
	}
}
